#include "MainMenu.h"
#include "../Engine.h"

MainMenu::MainMenu()
{
    screenName = "MainMenu";

    startBtn = new GUITextButton();
    optionsBtn = new GUITextButton();
    quitBtn = new GUITextButton();
    addChild(startBtn);
    addChild(optionsBtn);
    addChild(quitBtn);

    int verticalSpace = 75;

    startBtn->x = 50;
    startBtn->y = 50;
    startBtn->width = Engine::Instance().getScreenWidth() - 100;
    startBtn->height = 50;
    startBtn->addCallback(new SpecificCallback<MainMenu>(this, &MainMenu::onPressed));
    startBtn->setText("Adventure");
    form.add(startBtn);

    optionsBtn->x = 50;
    optionsBtn->y = startBtn->y + verticalSpace;
    optionsBtn->width = Engine::Instance().getScreenWidth() - 100;
    optionsBtn->height = 50;
    optionsBtn->addCallback(new SpecificCallback<MainMenu>(this, &MainMenu::onPressed));
    optionsBtn->setText("Options");
    form.add(optionsBtn);

    quitBtn->x = 50;
    quitBtn->y = optionsBtn->y + verticalSpace;
    quitBtn->width = Engine::Instance().getScreenWidth() - 100;
    quitBtn->height = 50;
    quitBtn->addCallback(new SpecificCallback<MainMenu>(this, &MainMenu::onPressed));
    quitBtn->setText("Quit");
    form.add(quitBtn);

    font = ContentManager::LoadContent<Texture2D>("Media/DejaVuSansMonoLeft.tga");

    // Modify current clear color
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

    // Generate a display list
    textDispList = glGenLists(1);
    glNewList(textDispList, GL_COMPILE);
        DrawHelper::Instance().drawStr2D("Choose a screen to start up:", 16, 16.0f, 16.0f, &Black, font, -6);
    glEndList();
}

void MainMenu::draw()
{
    //static float width = (float)Engine::Instance().screenWidth;
    //static float height = (float)Engine::Instance().screenHeight;

    // Draw a white backdrop.
    //DrawHelper::Instance().drawRect2D(0.0f, 0.0f, width, height, &Color(1.0f, 1.0f, 1.0f, 1.0f), true);

    glCallList(textDispList);
}

void MainMenu::update(Uint32* deltaTime)
{

}

void MainMenu::handleEvents(SDL_Event* event)
{
    if (event->type == SDL_KEYDOWN)
    {
        // Tab between gui elements in the form
        if (event->key.keysym.sym == SDLK_TAB)
        {
            // Note: We can't use key.keysym.mod == SDLK_LSHIFT unless the SDL unicode option is enabled.
            if (SDL_GetModState() & KMOD_SHIFT)
            {
                form.previous();
            }
            else
            {
                form.next();
            }
        }
        else if (event->key.keysym.sym == SDLK_UP || event->key.keysym.sym == SDLK_LEFT)
        {
            form.previous();
        }
        else if (event->key.keysym.sym == SDLK_DOWN || event->key.keysym.sym == SDLK_RIGHT)
        {
            form.next();
        }
        else if (event->key.keysym.sym == SDLK_RETURN)
        {
            form.onPressed();
        }
    }
    else if (event->type == SDL_KEYUP)
    {
        if (event->key.keysym.sym == SDLK_RETURN)
        {
            form.onReleased();
        }
    }
}

void MainMenu::onPressed(void* ref)
{
    if (ref == quitBtn)
    {
        // Siganl quit
        Engine::Instance().quit();
    }
    else if (ref == optionsBtn)
    {
        // Create and add screen
        /*
        // I would like to be able to do this so that the screen constructor
        // is only ever called if this screen does not already exist in the
        // list. However, there are some problems regarding passing a null
        // pointer through to PriorityList.
        OptionsMenu* options = 0; // Do not initialize screen quite yet
        Engine::Instance().addScreen2D(options);
        if (options == 0)
        {
            // Initialize options if the pointer was not changed in the process
            // being added as a screen.
            options = new OptionsMenu();
            std::cout << "Screen created after addScreen2D()." << std::endl;
        }
        */
        OptionsMenu* options = new OptionsMenu();
        Engine::Instance().addScreen2D(options);
        // Turn this screen off
        visible = updatable = eventable = false;
    }
    else if (ref == startBtn)
    {
        // Create and add screen
        Geogame* geo = new Geogame();
        Engine::Instance().addScreen2D(geo);
        // Turn this screen off
        visible = updatable = eventable = false;
    }
}
